package com.studyinghome.controller;

import com.studyinghome.entity.User;
import com.studyinghome.service.MongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Leslie (panxiang_work@163.com)
 * @website https://studyinghome.com
 * @create 2021-09-02 14:01
 */

@RestController
@RequestMapping("/userTemplate")
public class UserTemplateController {
    @Autowired
    private MongoService mongoService;

    @GetMapping("")
    public List<User> getAllUsers() {
        return mongoService.findAll();
    }

    @GetMapping("/{userId}")
    public User getByUserId(@PathVariable String userId) {
        return mongoService.findById(userId);
    }

    @PostMapping("")
    public User addNewUser(@RequestBody User user) {
        return mongoService.save(user);
    }

    @DeleteMapping("/{userId}")
    public String delete(@PathVariable String userId) {
        User user = new User();
        user.setId(userId);
        mongoService.deleteById(userId);
        return "deleted: " + userId;
    }

    @PutMapping("")
    public User update(@RequestBody User user) {
        return mongoService.save(user);
    }
}
